package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.SurveyDeviceInfo;
import com.enjoyor.soa.traffic.core.efoms.mapper.ISurveyDeviceInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.service.ISurveyDeviceInfoService;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class SurveyDeviceInfoService implements ISurveyDeviceInfoService{

	@Autowired
	private ISurveyDeviceInfoMapper surveyDeviceInfoMapper;
	@Override
	public Page getSurveyDeviceInfoPage(String devId, String devTypeCode, String devName, String devIp,
			String devStatusCode, String devFactory,String devAreaCode,String devDeptId, String searchFlag,
			Page page) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(devId, devTypeCode, devName, devIp, devStatusCode, devFactory, devAreaCode,devDeptId, searchFlag);
			page.setMapBean(pramT);
			return this.surveyDeviceInfoMapper.getSurveyDeviceInfoPage(page);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public List<SurveyDeviceInfo> getSurveyDeviceInfoList(String devId, String devTypeCode, String devName,
			String devIp, String devStatusCode, String devFactory,String devAreaCode,String devDeptId,
			String searchFlag) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(devId, devTypeCode, devName, devIp, devStatusCode, devFactory, devAreaCode,devDeptId, searchFlag);
			return this.surveyDeviceInfoMapper.getSurveyDeviceInfoList(pramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public SurveyDeviceInfo getSurveyDeviceInfo(String devId) {
		// TODO Auto-generated method stub
		return this.surveyDeviceInfoMapper.selectById(devId);
	}

	@Override
	public ResultPojo getSurveyCount(String devTypeCode, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getCountMap(devTypeCode, beginTime, endTime);
			int interCount = this.surveyDeviceInfoMapper.getInterUnNormalCount(pramT);//网络异常数量统计
			int dateCount = this.surveyDeviceInfoMapper.getDataUnNormalCount(pramT);//数据异常数量；
			int workordersCount = this.surveyDeviceInfoMapper.getworkerOrdersUnNormalCount(pramT);//维修异常统计
			int repairsCount = this.surveyDeviceInfoMapper.getRepairFlautCount(pramT);//报修异常统计；
			int totalSumCount = this.surveyDeviceInfoMapper.getDeviceTotalSum(pramT);//设备总数；
			int picMonitorCount = this.surveyDeviceInfoMapper.getPicMonitorFlautCount(pramT);//图片质量异常数统计；
			int warnFault = this.surveyDeviceInfoMapper.getWarnFaultCount(pramT);
			Map<String, Object> countMap = new HashMap<>();
			countMap.put("interCount", interCount);
			countMap.put("dateCount", dateCount);
			countMap.put("workordersCount", workordersCount);
			countMap.put("repairsCount", repairsCount);
			countMap.put("totalSumCount", totalSumCount);
			countMap.put("picMonitorCount", picMonitorCount);
			countMap.put("warnFault", warnFault);
			ResultPojo resultPojo = new ResultPojo();
			resultPojo.setResultList(countMap);
			return resultPojo;
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	//--------------------------------私有方法开始-------------------------------------//
	
	/**
	 * 获取查询Map；
	 * @param devId
	 * @param devTypeCode
	 * @param devName
	 * @param devIp
	 * @param devStatusCode
	 * @param devFactory
	 * @param devAreaCode
	 * @param searchFlag
	 * @return
	 */
	private Map<String,Object> getSelectMap(String devId, String devTypeCode, String devName, String devIp,
			String devStatusCode, String devFactory,String devAreaCode,String devDeptId,String searchFlag){
		Map<String, Object> map = new HashMap<>();
		map.put("devId", devId);
		map.put("devTypeCode", devTypeCode);
		map.put("devName", devName);
		map.put("devIp", devIp);
		map.put("devStatusCode", devStatusCode);
		map.put("devFactory", devFactory);
		map.put("devAreaCode", devAreaCode);
		map.put("devDeptId", devDeptId);
		map.put("searchFlag", searchFlag);
		return map;
	}
	
	/**
	 * 获取数量查询Map；
	 * @param devTypeCode
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	private Map<String, Object> getCountMap(String devTypeCode, String beginTime, String endTime){
		Map<String, Object> map = new HashMap<>();
		map.put("devTypeCode", devTypeCode);
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		return map;
	}

}
